//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Reader.Components;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;

namespace CommunityServer.ControlPanel.Reader
{
	/// <summary>
	/// Summary description for ManageArticles.
	/// </summary>
	public class ManageReaderAccess : BaseReaderAdminPage
	{
		#region Members

        protected CheckBox enableAds;
        protected CheckBox enableInline;
        protected CheckBoxList rolelist;
        protected ResourceLinkButton SaveButton;
		protected ConfigOKStatusMessage Status;
        protected FileOnlyStatusMessage FOStatus;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected ResourceControl Resourcecontrol1;
		protected HelpIcon Helpicon1;
		protected FormLabel Formlabel3;
		protected HelpIcon Helpicon3;
		protected FormLabel Formlabel2;
		protected HelpIcon Helpicon2;
		protected FormLabel Formlabel1;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
        protected PlaceHolder OptionHolder;
	
		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			InitializeComponent();
    		base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack )
			{
			    ReaderConfiguration config = ReaderConfiguration.Instance(false);
			    
                if(config.FileOnly)
                {
                    FOStatus.Visible = true;
                    OptionHolder.Visible = false;
                }
                else
                {
                    string everyone = CSContext.Current.Config.RolesConfiguration.Everyone;
                    string[] roles = Roles.GetRoleNames();
                    foreach(string role in roles)
                    {
                        if(string.Compare(role,everyone, true) != 0)
                        {
                            ListItem li = new ListItem(role,role.ToLower());
                            rolelist.Items.Add(li);
                        }
                    }

                    if(config.Roles != null)
                    {
                        foreach(string role in config.Roles)
                        {
                            ListItem li = rolelist.Items.FindByValue(role.ToLower());
                            if(li != null)
                                li.Selected = true;
                        }
                    }
                }
			}
        }

		private void InitializeComponent()
		{
            SaveButton.Click +=new EventHandler(SaveButton_Click);
		}

        private void SaveButton_Click(object sender, EventArgs e)
        {
            ReaderConfiguration config = ReaderConfiguration.Instance(false);
            if(!config.FileOnly)
            {
                ArrayList list = new ArrayList();
                foreach(ListItem li in rolelist.Items)
                {
                    if(li.Selected)
                    {
                        list.Add(li.Value);
                    }
                }

                config.Roles = (string[])list.ToArray(typeof(string));
                Status.Visible = true;
                config.Save();
            }
    }
    }
}
